package com.study.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Category;
import com.study.services.CategoryService;

@Controller
public class CategoryCotroller {
	@Autowired
	CategoryService service;
	
	/**
	 * 查询所有类别
	 * @return
	 */
	@PostMapping("/categoryAll")
	@ResponseBody
	public List<JSONObject> selectAll() {
		List<JSONObject> list = service.selectAll();
		return list;
	}
	
	@GetMapping("/querycategory")
	@ResponseBody
	public ModelAndView getForm4(HttpSession session) {
		ModelAndView mv = new ModelAndView("/goods/category");
		return mv;
	}
	
	/**
	 * 添加一级分类跳转添加页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/categoryAdd")
	public ModelAndView add(Integer id,Model model) {
		System.out.println(id+"=============");
		Category ca = service.findById(id);
		ModelAndView mv = new ModelAndView("/goods/categoryAdd");
		model.addAttribute("ca", ca);
		mv.addObject("good", new JSONObject());
		return mv;
	}
	
	/**
	 * 添加一级分类跳转添加页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/categoryAddOne")
	public ModelAndView addone(Integer id,Model model) {
		System.out.println(id+"=============");
		Category ca = new Category();
		ModelAndView mv = new ModelAndView("/goods/categoryAdd");
		ca.setCatId(0);
		model.addAttribute("ca", ca);
		mv.addObject("good", new JSONObject());
		return mv;
	}
	
	/**
	 * 修改类别名
	 * @param id
	 * @param upname
	 */
	@PostMapping("/editcatname")
	@ResponseBody
	public void editcategory(Integer id,String upname) {
		service.editcategory(id,upname);
		System.out.println("id是什么？？？"+id+"\n更改的名字为："+upname);
	}
	
	/**
	 * 删除分类
	 * @param request
 	 * @param id
	 */
	@PostMapping("/delca")
	@ResponseBody
	public void delete(HttpServletRequest request,Integer id) {
		System.out.println("进来了吗？"+id);
		service.deletecate(id);
	}
	
	/**
	 * 新增二级分类
	 * @param id
	 * @param lowname
	 */
	@PostMapping("/addca")
	@ResponseBody
	public void add(Integer id,String lowname) {
		System.out.println("id是什么呢？？？"+id+"\n下级分类呢是什么？？"+lowname);
		service.insert(id, lowname);
	}
	
	/**
	 * 新增一级分类
	 * @param id
	 * @param lowname
	 */
	@PostMapping("/addOne")
	@ResponseBody
	public void addOne(String lowname) {
		System.out.println("下级分类呢是什么？？"+lowname);
		service.insertOne(lowname);
	}
}
